package com.mspbots.sync.wise.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.sync.model.BoardOnly;
import com.mspbots.sync.model.PriorityOnly;
import com.mspbots.sync.wise.service.BoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * com.mspbots.sync.wise.controller.BoardController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/11/13
 */
@Log4j2
@RestController
@RequiredArgsConstructor
@RequestMapping("boards")
public class BoardController {

    private final BoardService service;

    @GetMapping("page")
    public Flux<BoardOnly> getBards(@RequestParam Integer tenantId) {
        Assert.notNull(tenantId, "Query params tenantId not null");
        return this.service.onGetWiseProperties(tenantId)
                .flatMapMany(this.service::getBoards);
    }

    @GetMapping("statuses")
    public Flux<JsonNode> getStatuses(@RequestParam Integer tenantId, @RequestParam Integer boardId) {
        Assert.notNull(tenantId, "Query params tenantId not null");
        Assert.notNull(boardId, "Query params boardId not null");
        return this.service.onGetWiseProperties(tenantId)
                .flatMapMany(config -> this.service.getStatuses(boardId));
    }

    @GetMapping("priorities")
    public Flux<PriorityOnly> getPriorities(@RequestParam Integer tenantId) {
        Assert.notNull(tenantId, "Query params tenantId not null");
        return this.service.onGetWiseProperties(tenantId)
                .flatMapMany(this.service::getPriorities);
    }
}
